package net.feling.mcloud.utils;

import java.util.HashMap;
import java.util.Map;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.BatteryManager;
import android.support.v4.app.NavUtils;

public class Config extends Application {

    private static final String TAG = "Config";
    private String cpufreq = "";
    private String domain = "";
    private String userid = "";
    private int batteryLevel;
    private Context myContext = this.getContext();

    private static long total_data = TrafficStats.getTotalRxBytes();

    public int getBatteryLevel() {
        return batteryLevel;
    }

    public void setBatteryLevel(int batteryLevel) {
        this.batteryLevel = batteryLevel;
    }

    private Map<String, String> urls = new HashMap<String, String>();
    private Context context;
    public String host;
    public int port;
    private static Config config;
    /*private Config() {
    }*/

	/*public static Config getGlobleConfig() {
		if (config == null) {
			config = new Config();
		}
		return config;
	}*/

    /**
     * urls中保存这代理上各接口的url地址
     *
     * @param key 接口url的key值
     * @return 对应接口的url地址
     */
    public String getUrl(String key) {
        if (urls.containsKey(key)) {
            return domain + urls.get(key);
        } else {
            return null;
        }
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getUserid() {
        return this.userid;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this.getApplicationContext();
        initUrls();
        initConnectArgs();
        //sendaBroadcast();
    }

    private void initUrls() {
        urls.put("getuserid", "/UserAction!getUserid");
        urls.put("keepalive", "/UserAction!keepAlive");
        urls.put("sendenergy", "/UserTaskAllocation!sendDeviceInfo");
    }

    private void initConnectArgs() {
        host = "192.168.1.102";
        port = 9898;
    }

    public Context getContext() {
        return context;
    }

    public void sendaBroadcast() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.BATTERY_CHANGED");
        sendBroadcast(intent);
    }

    public String getBatteryLeft() {
        String result = "0";
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        result = String.valueOf(level);
        if (result.equals("0")) {
            return "1";
        }
        return result;
    }

    /**
     * 判断网络是否连接
     *
     * @return 是否连接
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 剩余可用内存的大小 (RAM)
     * @param context context
     * @return 返回
     */
    public static long getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        long availmem = mi.availMem;
        availmem = availmem / (1024*1024);
        return availmem;
    }

    public static int getNetSpeed() {
        // 总的接收字节数
        long traffic_data = TrafficStats.getTotalRxBytes() - total_data;
        total_data = TrafficStats.getTotalRxBytes();
        return (int)traffic_data / 5;
    }

}
